//
//  PersonInfoAuthenVC.m
//  HxgProject
//
//  Created by hxg on 2018/3/7.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "PersonInfoAuthenVC.h"
#import "PersonInfoCell.h"

#import "HXPhotoView.h"
#import "HXPhotoPicker.h"
#import "UIViewController+HXExtension.h"

#import "TTPhotoPickerController.h"
#import "RDVTabBarController.h"

#import "AuthenService.h"
#import "PersonInfo.h"
#import "UIButton+Extention.h"
#import <IQKeyboardReturnKeyHandler.h>
#import "HxgTools.h"

@interface PersonInfoAuthenVC ()<UITableViewDelegate,UITableViewDataSource,HXAlbumListViewControllerDelegate,UIImagePickerControllerDelegate,UIScrollViewDelegate,UITextFieldDelegate>
{
    IQKeyboardReturnKeyHandler *returnKeyHandler;
}

@property (nonatomic, strong) UITableView *personInfoTableView;

// 配置数据源
@property (nonatomic, strong) NSMutableArray *personDataSource;

@property (nonatomic, strong) HXPhotoManager *manager;

// 照片是否上传的标记
@property (nonatomic, assign) BOOL isZhengMianUpLoad;
@property (nonatomic, assign) BOOL isFanMianUpLoad;
@property (nonatomic, assign) BOOL isAllMianUpLoad;

// 提交按钮
@property (nonatomic, strong) UIButton *submitBtn;


@end

@implementation PersonInfoAuthenVC


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    
    if (isPersonAuthenSuc) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    } else {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    [super viewWillDisappear:animated];
}

-(HXPhotoManager *)manager
{
    if (!_manager)
    {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
    }
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isZhengMianUpLoad = NO;
    self.isFanMianUpLoad = NO;
    self.isAllMianUpLoad = NO;
    
    [self setUpUI];
    
    returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
    
}

- (void)setUpUI
{
    self.navigationItem.title = @"个人信息认证";
    self.view.backgroundColor = defaultViewBgColor;
    
    [self.view addSubview:self.personInfoTableView];
    [self.personInfoTableView registerClass:[PersonInfoCell class]
                 forCellReuseIdentifier:@"PersonCell"];
    [self.personInfoTableView makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top);
        }
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height / 3 * 2));
    }];
    
    [self.view addSubview:self.submitBtn];
    [_submitBtn setTitle:@"提交认证" forState:UIControlStateNormal];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kPersonInfoAuthenRes])
    {
        _submitBtn.userInteractionEnabled = NO;//交互关闭
        _submitBtn.alpha = 0.4;//透明度
    } else {
        _submitBtn.userInteractionEnabled = YES;//交互打开
        _submitBtn.alpha = 1;//透明度
    }
    
    [_submitBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.personInfoTableView.mas_bottom).offset(5.0f);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width /3.0f, 70.0f));
    }];
}

-(UITableView *)personInfoTableView
{
    if (!_personInfoTableView)
    {
        _personInfoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreen_Width, kScreen_Height - 44.0f) style:UITableViewStyleGrouped];
        
        _personInfoTableView.delegate = self;
        _personInfoTableView.dataSource = self;
        _personInfoTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, KIphoneSize_Height(20.0f))];
        _personInfoTableView.tableFooterView = [[UIView alloc] init];
        _personInfoTableView.showsVerticalScrollIndicator = NO;
    }
    
    return _personInfoTableView;
}


#pragma mark - tableViewDelegate

//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (0 == section)
    {
        return 0;
    }
    
    return 0.01f;//section头部高度
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (0 == section) {
        return [[UIView alloc] init];
    }
    
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
        static NSString *identifier = @"PersonCell";
    
        PersonInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
        if (!cell)
        {
            cell = [[PersonInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        if (!isEmptyString(self.showPerson.name)) {
            if (indexPath.row == 0) {
                cell.autherContentField.text = self.showPerson.name;
            } else if (indexPath.row == 1) {
                cell.autherContentField.text = self.showPerson.idCard;
            } else if (indexPath.row == 2) {
                cell.autherContentField.text = self.showPerson.qq;
            } else if (indexPath.row == 3) {
                cell.autherContentField.text = self.showPerson.webChat;
            } else if (indexPath.row == 4) {
                cell.autherContentField.text = self.showPerson.chsiAccount;
            } else if (indexPath.row == 5) {
                cell.autherContentField.text = self.showPerson.chsiPwd;
            } else if (indexPath.row == 6) {
                cell.autherContentField.text = self.showPerson.operatorPwd;
            }
        }
    
        if (indexPath.row == 5
           || indexPath.row == 6 ) {
            cell.autherContentField.secureTextEntry = YES;
        }
    
        cell.autherContentField.delegate = self;
        cell.autherContentField.tag = indexPath.row;
        [cell.autherContentField addTarget:self
                                    action:@selector(getInputVal:)
                          forControlEvents:UIControlEventEditingChanged];
        
        cell.autherNameLbl.text = [self.personDataSource objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 1)
    {
        return 1;
    }
    
    return self.personDataSource.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - all Actions
- (void)getInputVal:(UITextField *)field
{
    switch (field.tag) {
        case 0:
            self.showPerson.name = field.text;
            NSLog(@"self.personInfo.name==>%@ === %@",self.showPerson.name,field.text);
            break;
        case 1:
            self.showPerson.idCard = field.text;
            break;
        case 2:
            self.showPerson.qq = field.text;
            break;
        case 3:
            self.showPerson.webChat = field.text;
            break;
        case 4:
            self.showPerson.chsiAccount = field.text;
            break;
        case 5:
            self.showPerson.chsiPwd = field.text;
            break;
        case 6:
            self.showPerson.operatorPwd = field.text;
            break;
        default:
            break;
    }
}


- (void)submitAction:(UIButton *)btn
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kPersonInfoAuthenRes])
    {
        [SVProgressHUD showErrorWithStatus:@"您已认证过,请勿重复提交!"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
    // 新增非空判断
    if (isEmptyString(self.showPerson.name)
        || isEmptyString(self.showPerson.operatorPwd)
        || isEmptyString(self.showPerson.qq)
        || isEmptyString(self.showPerson.idCard)
        || isEmptyString(self.showPerson.webChat)
        || isEmptyString(self.showPerson.chsiAccount)
        || isEmptyString(self.showPerson.chsiPwd))
    {
        [SVProgressHUD showErrorWithStatus:@"内容不能为空!"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"正在请求.."];
    __weak PersonInfoAuthenVC *weakSelf = self;
    AuthenService *authenService = [[AuthenService alloc] init];
    [authenService setUserInfoWithPerson:self.showPerson
                            withCallBack:^(BOOL isSuccess) {
                                [SVProgressHUD dismiss];
                                if (isSuccess)
                                {
                                    [[NSUserDefaults standardUserDefaults] setBool:YES
                                                                            forKey:kPersonInfoAuthenRes];
                                    [SVProgressHUD showSuccessWithStatus:@"操作成功!"];
                                    [SVProgressHUD dismissWithDelay:2.0f];
                                    [weakSelf.navigationController popViewControllerAnimated:YES];
                                }
                                else
                                {
                                    [[NSUserDefaults standardUserDefaults] setBool:NO
                                                                            forKey:kPersonInfoAuthenRes];
                                    [SVProgressHUD showErrorWithStatus:@"操作失败!"];
                                    [SVProgressHUD dismissWithDelay:2.0f];
                                }
                            }];
}


-(NSMutableArray *)personDataSource
{
    if (!_personDataSource)
    {
        _personDataSource = [[HxgTools sharedHxgTools] getProprotiesFormPlistFile:@"PersonInfo"];
    }
    return _personDataSource;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kPersonInfoAuthenRes])
    {
        return NO;
    }
    return YES;
}

#pragma mark - ###### getter、setter######

- (UIButton *)submitBtn
{
    if (!_submitBtn)
    {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitBtn.layer.cornerRadius = 6.0f;
        [_submitBtn setBackgroundColor:kCommonBtnBgColor];
        [_submitBtn addTarget:self
                       action:@selector(submitAction:)
             forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
